app_family.controller("controller_firstassess2", function($scope, $rootScope, httpRequest, $location) {
	$rootScope.$broadcast('currentModule', 'page_firstassess2');

	$scope.answerArr = []; // 答案code
	$scope.questionSelLength = 0; // 已答题数
	
	// 问题答案查询
	httpRequest.getSpiritQuestion('', function (data) {
		if (data.success === true) {
			$scope.questionList = data.obj;
			$scope.answerArr = new Array(data.obj.length);
		} else if (data.success === false) {
			$.dialog.showToast({msgTxt : data.msg});
		}
	});
	
	/**
	 * 答题 
	 */
	$scope.updateAnswer = function (questionIndex, code) {
		$scope.answerArr[questionIndex] = code;
		
		// 更新已答题数
		var tmp = 0;
		for (var i = 0; i < $scope.answerArr.length; i++) {
			if ($scope.answerArr[i] != '' && $scope.answerArr[i] != undefined 
					&& $scope.answerArr[i] != null) {
				tmp ++;
			}
		}
		$scope.questionSelLength = tmp;
		
		// 滚动至下一题
		$("html,body").animate({scrollTop : $("#qiestion_" + questionIndex).offset().top - 130}, 600);
	};
	
	/**
	 * 提交答案
	 */
	$scope.submitGo = function () {
		$scope.isSubmit = true;
		// 判断是否有未答的题
		for (var i = 0; i < $scope.answerArr.length; i++) {
			if ($scope.answerArr[i] == '' || $scope.answerArr[i] == undefined || $scope.answerArr[i] == null) {
				$.dialog.showToast({
					msgTxt : '为了更准备的为您评估，要做完剩下的题哦。',
					callbackOk : function () {
						$("html,body").animate({scrollTop : $("#qiestion_" + i).offset().top - 130}, 600);
					}
				});
				$scope.isSubmit = false;
				break;
			}
		}
		
		// 跳转到精神状态评估
		if ($scope.isSubmit) {
			// 保存答案到sessionStorage
			data_sessionStorage = JSON.parse(sessionStorage.getItem('data_sessionStorage'));
			data_sessionStorage.assInfo.ass_answer.firstassess2 = $scope.answerArr.toString();
			sessionStorage.setItem('data_sessionStorage', JSON.stringify(data_sessionStorage));
			
			$location.path('/firstassess3');
			$scope.$apply();
		}
	};
	
});